const JWT = require('jsonwebtoken');
const {
    tokenSecretKey,
    pwdSecretKey,
} = require('../config')

//生成token
function generateToken(id,scope) {

    const token = JWT.sign({
        id,
        scope
    }, tokenSecretKey)

    return token;

}

//验证token
function verifyToken(token) {

    try {
        JWT.verify(token, tokenSecretKey);
        let params = JWT.decode(token, tokenSecretKey);
        return params;
    } catch {
        return false;
    }
}


//加密密码
function encryptPwd(password) {

    const pwd = JWT.sign({
        password,
    }, pwdSecretKey)

    return pwd;

}

//验证密码
function decodePwd(password) {
    try {
        return JWT.decode(password, pwdSecretKey).password;
    } catch (e) {
        return false;
    }
}

// let token = JWT.sign({
//     password: 'root'
// }, pwdSecretKey);
// console.log(
//     token)
// console.log(decodePwd('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwYXNzd29yZCI6InJvb3QiLCJpYXQiOjE2Mzk5OTI2MjJ9.RmRBunF8JvUxGTug-7ZDgx7DGkYfJpEgPqGDDt3-CPY'));
module.exports = {
    generateToken,
    verifyToken,
    encryptPwd,
    decodePwd,
}